package com.github.lanif.core.proxy;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.URLUtil;
import com.github.lanif.core.exception.ServiceException;
import com.github.lanif.core.pojo.response.ResponseData;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * 后台转发前台请求避免跨域问题
 */
@RestController
@RequestMapping("/proxy")
public class ProxyController {
    @Resource
    private RestTemplate restTemplate;

    @Resource(name = "advancedRestTemplate")
    private RestTemplate advancedRestTemplate;

    /**
     * Get 请求转发
     * @param url
     * @param httpMethod
     * @return
     */
    @GetMapping("{url}")
    public ResponseData getRequest(@PathVariable String url, HttpMethod httpMethod) {
        Object data = forward(url, httpMethod, null);
        return ResponseData.success(data);
    }

    /**
     * Post JSON 请求
     * @param url
     * @param httpMethod
     * @param body
     * @return
     */
    @PostMapping(path = "{url}", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseData postJSON(@PathVariable String url, HttpMethod httpMethod, @RequestBody Object body) {
        Object data = forward(url, httpMethod, body);
        return ResponseData.success(data);
    }

    /**
     * 表单提交
     * @param url
     * @param httpMethod
     * @param body
     * @return
     */
    @PostMapping(path = "{url}", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public ResponseData postForm(@PathVariable String url, HttpMethod httpMethod, @RequestBody Object body) {
        Object data = forward(url, httpMethod, body);
        return ResponseData.success(data);
    }

    @PostMapping(path = "{url}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public void postMultipart(@PathVariable String url, HttpMethod httpMethod) {
    }

    private Object forward(String url, HttpMethod httpMethod, Object body) {
        if (ObjectUtil.isEmpty(url)) {
            throw new ServiceException(500, "请填写转向地址！");
        }
        String decode = Base64.decodeStr(url);
        try {
            HttpEntity httpEntity = new HttpEntity(body);
            URI uri = new URI(decode);
            ResponseEntity<Object> response =
                    advancedRestTemplate.exchange(uri, httpMethod, httpEntity, Object.class);

            return response.getBody();
        } catch (RestClientException e) {
            e.printStackTrace();
            throw new ServiceException(500, e.getMessage());
        } catch (URISyntaxException e) {
            throw new ServiceException(500, "请求地址异常!");
        }
    }

    public static void main(String[] args) {
        String url = "https%3A%2F%2Fwww.hutool.cn%2Fdocs%2F%23%2Fcore%2F%25E5%25B7%25A5%25E5%2585%25B7%25E7%25B1%25BB%2FURL%25E5%25B7%25A5%25E5%2585%25B7-URLUtil";
        String decode = URLUtil.decode(url);
        System.out.println(decode);
        String aa = Base64.decodeStr("aHR0cHM6Ly9naXRlZS5jb20vZmVhdHVyZV9ub3RpZmljYXRpb25z");
        System.out.println(aa);
    }
}
